#include "sqlite.hpp"

// 一行数据就会回调一次
int select_stu_callback(void *arg/*exec的第三个参数*/, int col_count/*列数*/, char **result/*多个列值*/, char **fields_name/*多个列名*/)
{
    std::vector<std::string> *arry = (std::vector<std::string> *)arg;
    arry->push_back(result[0]);
    return 0; // 必须返回0
}

int main()
{
    SqliteHelper helper("./test.db");
    helper.open();

    // 创建学生表
    const char *create_sql = "create table if not exists student(sn int primary key, name varchar(32), age int);";
    helper.exec(create_sql, nullptr, nullptr);

    // 插入数据
    // const char * insert_sql = "insert into student values(1,'张三',18), (2,'李四',18), (3,'王五',18);";
    // helper.exec(insert_sql, nullptr, nullptr);

    // 更新数据
    // const char * update_sql = "update student set name='张老三' where sn=1;";
    // helper.exec(update_sql, nullptr, nullptr);

    // 删除数据
    // const char * delete_sql = "delete from student where sn=3;";
    // helper.exec(delete_sql, nullptr, nullptr);

    // 查询数据
    const char *select_sql = "select name from student;";
    std::vector<std::string> arry;
    helper.exec(select_sql, select_stu_callback, &arry);
    for (auto name : arry)
    {
        std::cout << name << std::endl;
    }
    helper.close();

    return 0;
}